<?php

namespace app\h7_tool\controller\web\adv;

use xsframe\base\AdminBaseController;
use think\facade\Db;

class Article extends AdminBaseController
{
    protected $tableName = 'gm_arts_adv';

    public function index()
    {
        return redirect("/{$this->module}/web.adv/main");
    }

    public function main()
    {
        $keyword = $this->params['keyword'];

        $condition = [
            'uniacid' => $this->uniacid,
            'deleted' => 0,
            'type'    => 1,
        ];

        $cateid = $this->params['cateid'];
        if (is_numeric($cateid)) {
            $condition['cateid'] = $cateid;
        }

        $enabled = $this->params['enabled'];
        if (is_numeric($enabled)) {
            $condition['enabled'] = $enabled;
        }

        if (!empty($keyword)) {
            $condition[''] = Db::raw(" `title` like '%" . trim($keyword) . "%' ");
        }

        $list  = Db::name($this->tableName)->where($condition)->order('createtime desc,id asc')->page($this->pIndex, $this->pSize)->select()->toArray();
        $total = Db::name($this->tableName)->where($condition)->count();
        $pager = pagination2($total, $this->pIndex, $this->pSize);

        foreach ($list as &$item) {
            $item['category_name'] = Db::name('gm_arts_article_category')->where(['id' => $item['cateid']])->value('name');
        }
        unset($item);

        $categorys = Db::name('gm_arts_article_category')->where(['parentid' => 0, 'uniacid' => $this->uniacid, 'deleted' => 0])->order("displayorder desc")->select();

        $result = [
            'list'      => $list,
            'pager'     => $pager,
            'total'     => $total,
            'categorys' => $categorys,
        ];

        return $this->template('list', $result);
    }

    public function add()
    {
        return $this->post();
    }

    public function edit()
    {
        return $this->post();
    }

    public function post()
    {
        $id = $this->params['id'];

        if ($this->request->isPost()) {

            $data = array(
                "uniacid"      => $this->uniacid,
                "type"         => 1,
                "cateid"       => trim($this->params["cateid"]),
                "displayorder" => trim($this->params["displayorder"]),
                "title"        => trim($this->params["title"]),
                "thumb"        => trim($this->params["thumb"]),
                "link"         => trim($this->params["link"]),
                "enabled"      => intval($this->params["enabled"]),
            );

            if (!empty($id)) {
                Db::name($this->tableName)->where(["id" => $id])->update($data);
            } else {
                $data['createtime'] = time();
                Db::name($this->tableName)->insert($data);
            }

            $this->success(array("url" => webUrl("web.adv.article/main")));
        }

        $item      = Db::name($this->tableName)->where(['id' => $id])->find();
        $categorys = Db::name('gm_arts_article_category')->where(['deleted' => 0, 'parentid' => 0])->order("displayorder desc")->select();
        return $this->template('post', ['item' => $item, 'categorys' => $categorys]);
    }

}
